package com.sskj.b70.v.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.widget.TextView;

import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.b70.R;
import com.sskj.b70.common.App;
import com.sskj.b70.common.Constants;
import com.sskj.b70.common.adapter.BaseAdapter;
import com.sskj.b70.common.adapter.ViewHolder;
import com.sskj.b70.common.widget.ToolBarLayout;
import com.sskj.b70.m.bean.Entrust;
import com.sskj.b70.p.activity.EntrustPresenter;
import com.sskj.b70.utils.NumberUtil;
import com.sskj.b70.utils.ToastUtil;
import com.sskj.b70.v.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 当前委托列表
 */
public class EntrustActivity extends BaseActivity<EntrustPresenter> {

    @BindView(R.id.entrust_toolbar)
    ToolBarLayout entrustToolbar;
    @BindView(R.id.entrust_list)
    RecyclerView entrustList;
    @BindView(R.id.entrust_refresh)
    SmartRefreshLayout smartRefreshLayout;

    BaseAdapter<Entrust> adapter;
    private String code;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_entrust;
    }

    @Override
    public EntrustPresenter getPresenter() {
        return new EntrustPresenter();
    }


    @Override
    protected void initView() {

        smartRefreshLayout.setRefreshHeader(new MaterialHeader(this));
        smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            smartRefreshLayout.finishRefresh();
            mPresenter.getEntrustList();
        });
        entrustToolbar.mRightButton.setTextColor(getResources().getColor(R.color.tabNormalColor));
        entrustToolbar.setLeftButtonOnClickLinster(view -> finish());
        entrustToolbar.setRightButtonOnClickLinster(view -> {
            EntrustHistoryActivity.start(this, code);
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        entrustList.setLayoutManager(layoutManager);
        entrustList.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        adapter = new BaseAdapter<Entrust>(R.layout.item_recycler_entrust, new ArrayList<>(), entrustList, true) {
            @Override
            public void bind(ViewHolder holder, Entrust item) {
                if (item.getStockName() == null || item.getStockName().split("/").length == 1) {
                    item.setStockName("ETH/USDT");
                }
                SpannableString spannableString17 = new SpannableString(item.getStockName());
                RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(0.7F);
                ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(
                        Color.parseColor("#AABBCC"));
                StyleSpan styleSpan = new StyleSpan(Typeface.NORMAL);
                spannableString17.setSpan(relativeSizeSpan, item.getNumUnit().length() , item.getStockName().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                spannableString17.setSpan(foregroundColorSpan, item.getNumUnit().length() , item.getStockName().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                spannableString17.setSpan(styleSpan, item.getNumUnit().length(), item.getStockName().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                holder.setText(R.id.item_entrust_name, spannableString17)
                        .setText(R.id.item_entrust_type, item.getTradeType() == 1 ? App.INSTANCE.getString(R.string.strEntrustActivity3) : App.INSTANCE.getString(R.string.strEntrustActivity4))
                        .setTextColor(R.id.item_entrust_type, ContextCompat.getColor(EntrustActivity.this, item.getTradeType() == 1 ? R.color.market_down : R.color.market_up))
//                        .setText(R.id.item_entrust_time, TimeFormatUtil.SF_FORMAT_G.format(new Date(TimeFormatUtil.parseLongE(item.getCreateTime()))))
                        .setText(R.id.item_entrust_time, item.getCreateTime() + "")
                        .setText(R.id.item_entrust_price, item.getEntrustPrice() + "")
                        .setText(R.id.item_entrust_price_text, String.format(App.INSTANCE.getString(R.string.strEntrustActivity155), item.getPriceUnit()))
                        .setText(R.id.item_entrust_real_num_text, String.format(App.INSTANCE.getString(R.string.strEntrustActivity156), item.getNumUnit()))
                        .setText(R.id.item_entrust_real_num, item.getEntrustNum() + "")
                        .setText(R.id.item_entrust_num_text, String.format(App.INSTANCE.getString(R.string.strEntrustActivity158),item.getNumUnit()))
                        .setText(R.id.item_entrust_num, NumberUtil.keepMax(item.getEntrustNum(), 8));
                if (item.getBillPriceType() == 1 && item.getTradeType() == 1) {  //市价买入
                    holder.setText(R.id.item_entrust_num_text, App.INSTANCE.getString(R.string.strEntrustActivity14))
                            .setText(R.id.item_entrust_num, item.getTotalPrice() + "");
                } else { //其他
                    holder.setText(R.id.item_entrust_num_text, String.format(App.INSTANCE.getString(R.string.strEntrustActivity158),item.getNumUnit()))
                            .setText(R.id.item_entrust_num, item.getDealNum() + "");
                }
                if (item.getBillPriceType() == 1) {//市价
                    holder.setText(R.id.item_entrust_price, App.INSTANCE.getString(R.string.strEntrustActivity19));
                } else {
                    holder.setText(R.id.item_entrust_price, item.getEntrustPrice() + "");
                }
                TextView cancel = holder.getView(R.id.item_entrust_cancel);
                switch (item.getState()) {
                    case 0:
                    case 1:
                        cancel.setText(R.string.coin_cancel);
                        cancel.setOnClickListener(v -> mPresenter.cancelOrder(item.getTradeType(), item.getId() + ""));
                        cancel.setTextColor(getResources().getColor(R.color.bull_light_white));
                        break;
                    case 2:
                        cancel.setText(R.string.coin_finished);
                        cancel.setTextColor(getResources().getColor(R.color.bull_light_white));
                        break;
                    case 3:
                        cancel.setText(R.string.coin_cancled);
                        cancel.setTextColor(getResources().getColor(R.color.bull_light_white));
                        break;
                }
            }
        };
    }

    @Override
    protected void initData() {
        code = getIntent().getStringExtra(Constants.CODE);
        mPresenter.getEntrustList();
    }


    /**
     * 取消订单
     *
     * @param message 返回信息
     * @param success 是否成功取消
     */
    public void cancelOrder(String message, boolean success) {
        ToastUtil.showShort(message);
        if (success) {
            mPresenter.getEntrustList();
        }
    }

    /**
     * 设置列表
     *
     * @param data 数据源
     */
    public void setData(List<Entrust> data) {
        adapter.setNewData(data);
    }


    /**
     * 启动
     *
     * @param context context
     * @param
     */
    public static void start(Context context, String stockCode) {
        Intent intent = new Intent(context, EntrustActivity.class);
        intent.putExtra(Constants.CODE, stockCode);
        context.startActivity(intent);
    }

    public void stopRefresh() {

    }
}

